/**
 * Copyright (c) 2006-2012 Las Venturas Playground, LVP Mineground
 *
 * This program is free software: you can redistribute it and/or modify it under the terms of the
 * GNU General Public License as published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package mineground.core;

import java.lang.reflect.Method;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.PriorityQueue;

import mineground.util.Log;

/**
 * 
 * 
 * 
 */
public class MessageRouter {
    /**
     * 
     * 
     */
    private class RoutedMethod {
        /**
         * 
         * 
         */
        private ModuleControllerBase mInstance;
        
        /**
         * 
         * 
         */
        private Method mMethod;
        
        /**
         * 
         * 
         */
        private Priority mPriority;
        
        /**
         * 
         * 
         */
        public RoutedMethod(ModuleControllerBase controller, Method method, Priority prio) {
            mInstance = controller;
            mMethod = method;
            mPriority = prio;
        }
        
        /**
         * 
         * 
         */
        public String getCanonicalName() {
            return mInstance.getClass().getName() + "::" + mMethod.getName();
        }
        
        /**
         * 
         * 
         */
        public boolean invoke(Object... params) {
            try {
                mMethod.invoke(mInstance, params);
            } catch (Exception e) {
                return false;
            }
            return true;
        }
    }
    
    /**
     * 
     * 
     */
    private class RoutedMethodComparer implements Comparator<RoutedMethod> {
        /**
         * Compare two RoutedMessage instances based on their priority, which should be sorted in
         * ascending order (0 = Highest, 4 = Lowest).
         */
        @Override
        public int compare(RoutedMethod left, RoutedMethod right) {
            // @TODO Implement this method.
            return 0;
        }
    }
    
    /**
     * 
     * 
     */
    private HashMap<Message, PriorityQueue<RoutedMethod> > mMessageLists = new
        HashMap<Message, PriorityQueue<RoutedMethod> >();
    
    /**
     * 
     * 
     */
    public void registerHandler(Message message, ModuleControllerBase controller, Method method,
                                Priority priority) {
        RoutedMethod routedMethod = new RoutedMethod(controller, method, priority);
        synchronized (mMessageLists) {
            if (mMessageLists.containsKey(message) == false) {
                mMessageLists.put(message,
                    new PriorityQueue<RoutedMethod>(5, new RoutedMethodComparer()));
            }
            
            mMessageLists.get(message).add(routedMethod);
        }
    }
    
    /**
     * 
     * 
     */
    public void invokeHandlers(Message message, Object... params) {
        // @TODO Implement this method properly.
        synchronized (mMessageLists) {
            if (mMessageLists.containsKey(message) == false)
                return;
            
            PriorityQueue<RoutedMethod> handlers = mMessageLists.get(message);
            for (Iterator<RoutedMethod> iter = handlers.iterator(); iter.hasNext(); ) {
                RoutedMethod method = iter.next();
                if (method.invoke(params) == false) {
                    Log.warning("Unable to invoke the handler '%s' for message '%s'.",
                        method.getCanonicalName(), message.toString());
                }
            }
        }
    }
    
    /**
     * 
     * 
     */
    public void unregisterHandlersForController(ModuleControllerBase controller) {
        // @TODO Implement this method
    }
}
